package com.test.demos.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.test.demos.pojo.vo.UserVO;
import com.test.demos.service.UsersService;
import com.test.demos.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    /**
     * 获取用户信息
     */
    @SaCheckLogin
    @GetMapping()
    public Result getUserInfo() {
      if (!StpUtil.isLogin()) {
            return Result.error("未登录");
        }

        return Result.ok(usersService.getUserInfo());
    }

    /**
     * 删除用户
     */
    @SaCheckRole("admin")
    @DeleteMapping()
    public Result deleteUser(@PathVariable("userId") Integer userId) {
        return Result.ok(usersService.deleteUser(userId));
    }
    /**
     * 查询所有用户
     */
    @SaCheckRole("admin")
    @GetMapping("all")
    public Result getAllUsers() {
        List<UserVO> userVOS = usersService.queryAllUsers();
        System.out.println("userVOS = " + userVOS);

        return Result.ok(userVOS);
    }

}
